home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard CTB Toolkit 1.0b2 / Source Code / CTBRecvBytes.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  3.4 KB  |  126 lines  |  [TEXT/MPS ]

  1. (*
  2.     CTBRecvBytes([n]) -- Receive n characters, waiting until they're available if necessary.
  3.         Default to all available characters. Return them as a list of numbers.
  4.  
  5.     To compile and link this file using Macintosh Programmer's Workshop,
  6.  
  7.         pascal -w CTBRecvBytes.p
  8.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=2759 -sn Main=CTBRecvBytes ∂
  9.             CTBRecvBytes.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  10.  
  11.     © Copyright 1990 by Apple Computer, Inc.
  12.  
  13.     Initial coding 2/90 by Harry R. Chesley.
  14. *)
  15.  
  16. {$R-}
  17.  
  18. {$S CTBRecvBytes }     { Segment name must be the same as the command name. }
  19.  
  20. unit DummyUnit;
  21.  
  22. interface
  23.  
  24. uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
  25.  
  26. procedure EntryPoint(paramPtr: XCmdPtr);
  27.     
  28. implementation
  29.  
  30. procedure CTBRecvBytes(paramPtr: XCmdPtr); forward;
  31.  
  32. procedure EntryPoint(paramPtr: XCmdPtr);
  33.  
  34.     begin
  35.         CTBRecvBytes(paramPtr);
  36.     end;
  37.  
  38. procedure CTBRecvBytes(paramPtr: XCmdPtr);
  39.  
  40.     {$I CTBUtil.inc}
  41.  
  42.     var toRead, haveRead: longInt;
  43.         l: longInt;
  44.         h: Handle;
  45.         p: Ptr;
  46.         theBuf: InputBufferHandle;
  47.         sizes: CMBufferSizes;
  48.         status: CMStatFlags;
  49.         s: Str255;
  50.  
  51.     procedure Fail(errMsg: Str255); { set theResult and quit }
  52.         begin
  53.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  54.             exit(CTBRecvBytes);
  55.         end;
  56.  
  57.     begin
  58.         { Check the number of parameters. }
  59.         if paramPtr^.paramCount > 1 then Fail('Invalid parameter count');
  60.  
  61.         { Make sure the Comm Toolbox is ready. }
  62.         CTBReady;
  63.         { And there's a connection tool. }
  64.         EnsurePresent(connectionTool);
  65.         { And it's open. }
  66.         EnsureOpen;
  67.         { Get the input buffer. }
  68.         theBuf := InputBufferHandle(CMGetUserData(Globals^^.connHand));
  69.         { If there's any termination information around, get rid of it. }
  70.         if theBuf^^.termString <> nil then
  71.             begin
  72.                 DisposHandle(theBuf^^.termString);
  73.                 theBuf^^.termString := nil;
  74.                 theBuf^^.timeOut := -1;
  75.             end;
  76.  
  77.         { Figure out how much to read. }
  78.         if ParmPresent(1) then toRead := GetLongParm(1)
  79.         else if CMStatus(Globals^^.connHand,sizes,status) = noErr then
  80.             toRead := sizes[cmDataIn]+theBuf^^.amountLeft
  81.         else toRead:= theBuf^^.amountLeft;
  82.         { Allocate the space to read it into. }
  83.         h := NewHandle(toRead);
  84.         HLock(h);
  85.         { Get the input. }
  86.         haveRead := 0;
  87.         if toRead > 0 then
  88.             begin
  89.                 { Get what we can from the input buffer. }
  90.                 haveRead := min(toRead,theBuf^^.amountLeft);
  91.                 if haveRead > 0 then
  92.                     begin
  93.                         BlockMove(theBuf^^.bufferPtr,h^,haveRead);
  94.                         theBuf^^.amountLeft := theBuf^^.amountLeft - haveRead;
  95.                         toRead := toRead - haveRead;
  96.                     end;
  97.                 { Get the rest from outside (yes, even if it means suspending). }
  98.                 if toRead > 0 then haveRead := haveRead + ReadFromConn(Ptr(ord4(h^)+haveRead),toRead);
  99.             end;
  100.  
  101.         { Convert the input to a string. }
  102.         paramPtr^.returnValue := NewHandle(0);
  103.         if paramPtr^.returnValue = nil then Fail('Out of memory');
  104.         p := h^;
  105.         { Convert each byte. }
  106.         for l := 1 to haveRead do
  107.             begin
  108.                 LongToStr(paramPtr,BAND(p^,$FF),s);
  109.                 s := Concat(s,',');
  110.                 if PtrAndHand(pointer(ord4(@s)+1),paramPtr^.returnValue,length(s)) <> noErr then
  111.                     begin
  112.                         DisposHandle(h);
  113.                         DisposHandle(paramPtr^.returnValue);
  114.                         Fail('PtrAndHand failed');
  115.                     end;
  116.                 p := pointer(ord4(p)+1);
  117.             end;
  118.         { Replace the last comma with a period (if there was a last comma). }
  119.         p := pointer(ord4(paramPtr^.returnValue^)+GetHandleSize(paramPtr^.returnValue)-1);
  120.         if p <> paramPtr^.returnValue^ then p^ := 0;
  121.         { Dispose of the buffer. }
  122.         DisposHandle(h);
  123.     end;
  124.  
  125. end.
  126.